//
//  FKShareTool.m
//  智慧水利平台
//
//  Created by Macbook Pro on 2020/6/13.
//  Copyright © 2020 jxwdyf. All rights reserved.
//

#import "FKShareTool.h"
#import <SDWebImageDownloader.h>
#import <AFNetworking/AFNetworking.h>
#import <GLKit/GLKit.h>


@implementation FKShareTool

FKSingletonM

/** open GL 截屏 */
+ (UIImage*)takePicture
{
    UIWindow *mainWindow = [UIApplication sharedApplication].delegate.window;
    UIGraphicsBeginImageContextWithOptions(mainWindow.frame.size, NO, 0);
    [mainWindow drawViewHierarchyInRect:mainWindow.frame afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (void)shareCurrentScreen:(UIViewController *)shareVC
{
//    UIImage *image = [[UIApplication sharedApplication].delegate.window fk_snapshotImage];
    UIImage *image = [self takePicture];
    [self shareWithImage:image shareVC:shareVC];
}

+ (void)shareWithImage:(UIImage *)image shareVC:(UIViewController *)shareVC
{
    if (!image) return;
    if (!shareVC) return;
    
    [self shareItems:@[image] shareVC:shareVC];
}

+ (void)shareWithImagePaths:(NSArray *)imagePaths shareVC:(UIViewController *)shareVC
{
    __block NSMutableArray *images = [NSMutableArray array];
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
    [SVProgressHUD show];
    for (NSString *imagePath in imagePaths) {
        dispatch_group_enter(group);
        [downloader downloadImageWithURL:[NSURL URLWithString:imagePath] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            if (image) [images addObject:image];
            dispatch_group_leave(group);
        }];
    }

    dispatch_group_notify(group, queue, ^{
        if (images.count > 0) {
            [self shareItems:images shareVC:shareVC];
        } else {
            [SVProgressHUD showInfoWithStatus:@"暂无图片"];
        }
    });
}

+ (void)shareWithVideoUrl:(NSString *)videoUrl shareVC:(UIViewController *)shareVC
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSURL *URL = [NSURL URLWithString:videoUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    [SVProgressHUD show];
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    //            NSLog(@"File downloaded to: %@", filePath);
        [SVProgressHUD dismiss];
        [self shareItems:@[filePath] shareVC:shareVC];
    }];
    [downloadTask resume];
}

+ (void)shareItems:(NSArray *)items shareVC:(UIViewController *)shareVC
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
        activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook,
                                             UIActivityTypePostToTwitter,
                                             UIActivityTypePostToWeibo,
                                             UIActivityTypeMessage,
                                             UIActivityTypeMail,
                                             UIActivityTypePrint,
                                             UIActivityTypeCopyToPasteboard,
                                             UIActivityTypeAssignToContact,
                                             UIActivityTypeSaveToCameraRoll,
                                             UIActivityTypeAddToReadingList,
                                             UIActivityTypePostToFlickr,
                                             UIActivityTypePostToVimeo,
                                             UIActivityTypePostToTencentWeibo,
                                             UIActivityTypeAirDrop,
                                             UIActivityTypeOpenInIBooks];
        [shareVC presentViewController:activityVC animated:YES completion:nil];
    });
}

@end
